<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2021/1/9
 * Time: 11:27
 */
namespace App\Http\Controllers\Admin;

use App\Constant\ErrorConstant;
use App\Service\Admin\ManagerService;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;

class ManagerController extends BaseController
{
    protected $service;

    public function __construct(Request $request, ManagerService $service)
    {
        parent::__construct($request);

        $this->service  =   $service;
    }

    /**
     * 管理员列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listManager()
    {
        $page   =   (int) $this->request->input('page', 1);
        $limit  =   $this->request->input('page_size', null);

        [$flag, $result]    =   $this->service->managerList($limit,$page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::SYSTEM_MANAGER_LIST_ERROR, $result);
        }
    }

    /**
     * 添加管理员
     * @return \Illuminate\Http\JsonResponse
     */
    public function addManager()
    {
        $validator  =   Validator::make($this->request->post(), [
            'account'   =>  'required|string',
            'nick_name' =>  'required|string',
            'password'  =>  'required|string'
        ], [
            'account.required'  =>  '管理员账号不能为空',
            'account.string'    =>  '管理账号必须为字符',
            'nick_name.required'=>  '管理员昵称不能为空',
            'nick_name.string'  =>  '管理员昵称必须为字符',
            'password.required' =>  '管理员密码不能为空',
            'password.string'   =>  '管理员密码必须为字符'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::SYSTEM_MANAGER_ADD_ERROR, $validator->errors()->first());
        }

        $account    =   $this->request->post('account');
        $nick_name  =   $this->request->post('nick_name');
        $password   =   $this->request->post('password');

        [$flag, $result]    =   $this->service->managerAdd($account, $nick_name, $password);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::SYSTEM_MANAGER_ADD_ERROR, $result);
        }
    }

    /**
     * 删除系统管理员
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteManager()
    {
        $validator  =   Validator::make($this->request->post(), [
            'manager_id'    =>  'required|integer|exists:mini_box_system,id'
        ], [
            'manager_id.required'   =>  '管理员ID不能为空',
            'manager_id.integer'    =>  '管理员ID必须为整型',
            'manager_id.exists'     =>  '管理员不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::SYSTEM_MANAGER_DELETE_ERROR, $validator->errors()->first());
        }

        $manager_id =   (int) $this->request->post('manager_id');
        [$flag, $result]    =   $this->service->managerDelete($manager_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::SYSTEM_MANAGER_DELETE_ERROR, $result);
        }
    }

    /**
     * 修改管理员密码
     * @return \Illuminate\Http\JsonResponse
     */
    public function passwordManager()
    {
        $validator  =   Validator::make($this->request->post(), [
            'manager_id'    =>  'required|integer|exists:mini_box_system,id',
            'old_password'  =>  'required|string',
            'new_password'  =>  'required|string',
        ], ['manager_id.required'   =>  '管理员ID不能为空',
            'manager_id.integer'    =>  '管理员ID必须为整型',
            'manager_id.exists'     =>  '管理员不存在',
            'old_password.required' =>  '旧密码不能为空',
            'old_password.string'   =>  '旧密码必须为字符',
            'new_password.required' =>  '新密码不能为空',
            'new_password.string'   =>  '新密码必须为字符'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::SYSTEM_MANAGER_PASSWORD_ERROR, $validator->errors()->first());
        }

        $manager_id  =   (int) $this->request->post('manager_id');
        $old_password=  $this->request->post('old_password');
        $new_password=  $this->request->post('new_password');

        [$flag, $result]    =   $this->service->managerPassword($manager_id, $old_password, $new_password);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::SYSTEM_MANAGER_PASSWORD_ERROR, $result);
        }
    }
}
